home *** CD-ROM | disk | FTP | other *** search
/ SGI Freeware 1999 August / SGI Freeware 1999 August.iso / dist / fw_xemacs.idb / usr / freeware / lib / xemacs-20.4 / lisp / x11 / x-faces.el.z / x-faces.el
Encoding:
Text File  |  1998-05-21  |  26.9 KB  |  647 lines

  1. ;;; x-faces.el --- X-specific face frobnication, aka black magic.
  2.  
  3. ;;; Copyright (C) 1992, 1993, 1994 Free Software Foundation, Inc.
  4. ;;; Copyright (C) 1995, 1996 Ben Wing.
  5.  
  6. ;; Author: Jamie Zawinski
  7. ;; Modified by:  Chuck Thompson
  8. ;; Modified by:  Ben Wing
  9. ;; Modified by:  Martin Buchholz
  10.  
  11. ;; This file is part of XEmacs.
  12.  
  13. ;; XEmacs is free software; you can redistribute it and/or modify it
  14. ;; under the terms of the GNU General Public License as published by
  15. ;; the Free Software Foundation; either version 2, or (at your option)
  16. ;; any later version.
  17.  
  18. ;; XEmacs is distributed in the hope that it will be useful, but
  19. ;; WITHOUT ANY WARRANTY; without even the implied warranty of
  20. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  21. ;; General Public License for more details.
  22.  
  23. ;; You should have received a copy of the GNU General Public License
  24. ;; along with XEmacs; see the file COPYING.  If not, write to the 
  25. ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  26. ;; Boston, MA 02111-1307, USA.
  27.  
  28. ;; This file does the magic to parse X font names, and make sure that the
  29. ;; default and modeline attributes of new frames are specified enough.
  30. ;;
  31. ;;  The resource-manager syntax for faces is
  32. ;;
  33. ;;     Emacs.bold.attributeFont:        font-name
  34. ;;     Emacs.bold.attributeForeground:    fg
  35. ;;     Emacs.bold.attributeBackground:    bg
  36. ;;     Emacs.bold.attributeBackgroundPixmap:    file
  37. ;;     Emacs.bold.attributeUnderline:        true/false
  38. ;;     Emacs.bold.attributeStrikethru:    true/false
  39. ;;
  40. ;;  You can specify the properties of a face on a per-frame basis.  For 
  41. ;;  example, to have the "isearch" face use a red foreground on frames
  42. ;;  named "emacs" (the default) but use a blue foreground on frames that
  43. ;;  you create named "debugger", you could do
  44. ;;
  45. ;;     Emacs*emacs.isearch.attributeForeground:    red
  46. ;;     Emacs*debugger.isearch.attributeForeground:    blue
  47. ;;
  48. ;;  Generally things that make faces won't set any of the face attributes if
  49. ;;  you have already given them values via the resource database.  You can
  50. ;;  also change this stuff from your .emacs file, by using the functions
  51. ;;  set-face-foreground, set-face-font, etc.  See the code in this file, and
  52. ;;  in faces.el.
  53.  
  54. (defconst x-font-regexp nil)
  55. (defconst x-font-regexp-head nil)
  56. (defconst x-font-regexp-head-2 nil)
  57. (defconst x-font-regexp-weight nil)
  58. (defconst x-font-regexp-slant nil)
  59. (defconst x-font-regexp-pixel nil)
  60. (defconst x-font-regexp-point nil)
  61. (defconst x-font-regexp-foundry-and-family nil)
  62. (defconst x-font-regexp-registry-and-encoding nil)
  63. (defconst x-font-regexp-spacing nil)
  64.  
  65. ;;; Regexps matching font names in "Host Portable Character Representation."
  66. ;;;
  67. (let ((-         "[-?]")
  68.       (foundry        "[^-]*")
  69.       (family         "[^-]*")
  70.       (weight        "\\(bold\\|demibold\\|medium\\|black\\)")    ; 1
  71. ;     (weight\?        "\\(\\*\\|bold\\|demibold\\|medium\\|\\)")    ; 1
  72.       (weight\?        "\\([^-]*\\)")                    ; 1
  73.       (slant        "\\([ior]\\)")                    ; 2
  74. ;     (slant\?        "\\([ior?*]?\\)")                ; 2
  75.       (slant\?        "\\([^-]?\\)")                    ; 2
  76. ;     (swidth        "\\(\\*\\|normal\\|semicondensed\\|\\)")    ; 3
  77.       (swidth        "\\([^-]*\\)")                    ; 3
  78. ;     (adstyle        "\\(\\*\\|sans\\|\\)")                ; 4
  79.       (adstyle        "\\([^-]*\\)")                    ; 4
  80.       (pixelsize    "\\(\\*\\|[0-9]+\\)")                ; 5
  81.       (pointsize    "\\(\\*\\|0\\|[0-9][0-9]+\\)")            ; 6
  82. ;      (resx        "\\(\\*\\|[0-9][0-9]+\\)")            ; 7
  83. ;      (resy        "\\(\\*\\|[0-9][0-9]+\\)")            ; 8
  84.       (resx        "\\([*0]\\|[0-9][0-9]+\\)")            ; 7
  85.       (resy        "\\([*0]\\|[0-9][0-9]+\\)")            ; 8
  86.       (spacing        "[cmp?*]")
  87.       (avgwidth        "\\(\\*\\|[0-9]+\\)")                ; 9
  88.       (registry        "[^-]*") ; some fonts have omitted registries
  89. ;      (encoding    ".+")        ; note that encoding may contain "-"...
  90.       (encoding    "[^-]+")        ; false!
  91.       )
  92.   (setq x-font-regexp
  93.     (purecopy 
  94.      (concat "\\`\\*?[-?*]"
  95.          foundry - family - weight\? - slant\? - swidth - adstyle -
  96.          pixelsize - pointsize - resx - resy - spacing - avgwidth -
  97.          registry - encoding "\\'"
  98.          )))
  99.   (setq x-font-regexp-head
  100.     (purecopy
  101.           (concat "\\`[-?*]" foundry - family - weight\? - slant\?
  102.           "\\([-*?]\\|\\'\\)")))
  103.   (setq x-font-regexp-head-2
  104.     (purecopy
  105.           (concat "\\`[-?*]" foundry - family - weight\? - slant\?
  106.           - swidth - adstyle - pixelsize - pointsize
  107.           "\\([-*?]\\|\\'\\)")))
  108.   (setq x-font-regexp-slant (purecopy (concat - slant -)))
  109.   (setq x-font-regexp-weight (purecopy (concat - weight -)))
  110.   ;; if we can't match any of the more specific regexps (unfortunate) then
  111.   ;; look for digits; assume 2+ digits is 10ths of points, and 1-2 digits
  112.   ;; is pixels.  Bogus as hell.
  113.   (setq x-font-regexp-pixel (purecopy "[-?*]\\([0-9][0-9]?\\)[-?*]"))
  114.   (setq x-font-regexp-point (purecopy "[-?*]\\([0-9][0-9]+\\)[-?*]"))
  115.   ;; the following two are used by x-font-menu.el.
  116.   (setq x-font-regexp-foundry-and-family
  117.     (purecopy (concat "\\`[-?*]" foundry - "\\(" family "\\)" -)))
  118.   (setq x-font-regexp-registry-and-encoding
  119.     (purecopy (concat - "\\(" registry "\\)" - "\\(" encoding "\\)\\'")))
  120.   (setq x-font-regexp-spacing
  121.     (purecopy (concat - "\\(" spacing "\\)" - avgwidth
  122.               - registry - encoding "\\'")))
  123.   )
  124.  
  125. ;; A "loser font" is something like "8x13" -> "8x13bold".
  126. ;; These are supported only through extreme generosity.
  127. (defconst x-loser-font-regexp (purecopy "\\`[0-9]+x[0-9]+\\'"))
  128.  
  129. (defun x-frob-font-weight (font which)
  130.   (if (font-instance-p font) (setq font (font-instance-name font)))
  131.   (cond ((null font) nil)
  132.     ((or (string-match x-font-regexp font)
  133.          (string-match x-font-regexp-head font)
  134.          (string-match x-font-regexp-weight font))
  135.      (concat (substring font 0 (match-beginning 1)) which
  136.          (substring font (match-end 1))))
  137.     ((string-match x-loser-font-regexp font)
  138.      (concat font which))
  139.     (t nil)))
  140.  
  141. (defun x-frob-font-slant (font which)
  142.   (if (font-instance-p font) (setq font (font-instance-name font)))
  143.   (cond ((null font) nil)
  144.     ((or (string-match x-font-regexp font)
  145.          (string-match x-font-regexp-head font))
  146.      (concat (substring font 0 (match-beginning 2)) which
  147.          (substring font (match-end 2))))
  148.     ((string-match x-font-regexp-slant font)
  149.      (concat (substring font 0 (match-beginning 1)) which
  150.          (substring font (match-end 1))))
  151.     ((string-match x-loser-font-regexp font)
  152.      (concat font which))
  153.     (t nil)))
  154.  
  155. (defun try-font-name (name &optional device)
  156.   ;; yes, name really should be here twice.
  157.   (and name (make-font-instance name device t) name))
  158.  
  159. (defun x-make-font-bold (font &optional device)
  160.   "Given an X font specification, this attempts to make a `bold' font.
  161. If it fails, it returns nil."
  162.   ;; Certain Type1 fonts know "bold" as "black"...
  163.   (or (try-font-name (x-frob-font-weight font "bold") device)
  164.       (try-font-name (x-frob-font-weight font "black") device)
  165.       (try-font-name (x-frob-font-weight font "demibold") device)))
  166.  
  167. (defun x-make-font-unbold (font &optional device)
  168.   "Given an X font specification, this attempts to make a non-bold font.
  169. If it fails, it returns nil."
  170.   (try-font-name (x-frob-font-weight font "medium") device))
  171.  
  172. (defcustom *try-oblique-before-italic-fonts* nil
  173.   "*If nil, italic fonts are searched before oblique fonts.
  174. If non-nil, oblique fonts are tried before italic fonts.  This is mostly
  175. applicable to adobe-courier fonts"
  176.   :type 'boolean
  177.   :tag "Try Oblique Before Italic Fonts"
  178.   :group 'x)
  179.  
  180. (defun x-make-font-italic (font &optional device)
  181.   "Given an X font specification, this attempts to make an `italic' font.
  182. If it fails, it returns nil."
  183.   (if *try-oblique-before-italic-fonts*
  184.       (or (try-font-name (x-frob-font-slant font "o") device)
  185.       (try-font-name (x-frob-font-slant font "i") device))
  186.     (or (try-font-name (x-frob-font-slant font "i") device)
  187.     (try-font-name (x-frob-font-slant font "o") device))))
  188.  
  189. (defun x-make-font-unitalic (font &optional device)
  190.   "Given an X font specification, this attempts to make a non-italic font.
  191. If it fails, it returns nil."
  192.   (try-font-name (x-frob-font-slant font "r") device))
  193.  
  194. (defun x-make-font-bold-italic (font &optional device)
  195.   "Given an X font specification, this attempts to make a `bold-italic' font.
  196. If it fails, it returns nil."
  197.   ;; This is haired up to avoid loading the "intermediate" fonts.
  198.   (or (try-font-name
  199.        (x-frob-font-slant (x-frob-font-weight font "bold") "i") device)
  200.       (try-font-name
  201.        (x-frob-font-slant (x-frob-font-weight font "bold") "o") device)
  202.       (try-font-name
  203.        (x-frob-font-slant (x-frob-font-weight font "black") "i") device)
  204.       (try-font-name
  205.        (x-frob-font-slant (x-frob-font-weight font "black") "o") device)
  206.       (try-font-name
  207.        (x-frob-font-slant (x-frob-font-weight font "demibold") "i") device)
  208.       (try-font-name
  209.        (x-frob-font-slant (x-frob-font-weight font "demibold") "o") device)))
  210.  
  211. (defun x-font-size (font)
  212.   "Return the nominal size of the given font.
  213. This is done by parsing its name, so it's likely to lose.
  214. X fonts can be specified (by the user) in either pixels or 10ths of points,
  215.  and this returns the first one it finds, so you have to decide which units
  216.  the returned value is measured in yourself..."
  217.   (if (font-instance-p font) (setq font (font-instance-name font)))
  218.   (cond ((or (string-match x-font-regexp font)
  219.          (string-match x-font-regexp-head-2 font))
  220.      (string-to-int (substring font (match-beginning 6) (match-end 6))))
  221.     ((or (string-match x-font-regexp-pixel font)
  222.          (string-match x-font-regexp-point font))
  223.      (string-to-int (substring font (match-beginning 1) (match-end 1))))
  224.     (t nil)))
  225.  
  226. ;; Given a font name, this function returns a list describing all fonts
  227. ;; of all sizes that otherwise match the given font spec.  Each element
  228. ;; in the list is a list of three items: the pixel size of the font,
  229. ;; the point size (in 1/10ths of a point) of the font, and the fully-
  230. ;; qualified font name.  The first two values may be zero; this
  231. ;; refers to a scalable font.
  232.  
  233. (defun x-available-font-sizes (font device)
  234.   (if (font-instance-p font) (setq font (font-instance-name font)))
  235.   (cond ((string-match x-font-regexp font)
  236.      ;; turn pixelsize, pointsize, and avgwidth into wildcards
  237.      (setq font
  238.            (concat (substring font 0 (match-beginning 5)) "*"
  239.                (substring font (match-end 5) (match-beginning 6)) "*"
  240.                (substring font (match-end 6) (match-beginning 9)) "*"
  241.                (substring font (match-end 9) (match-end 0)))))
  242.     ((string-match x-font-regexp-head-2 font)
  243.      ;; turn pixelsize and pointsize into wildcards
  244.      (setq font
  245.            (concat (substring font 0 (match-beginning 5)) "*"
  246.                (substring font (match-end 5) (match-beginning 6)) "*"
  247.                (substring font (match-end 6) (match-end 0)))))
  248.     ((string-match  "[-?*]\\([0-9]+\\)[-?*]" font)
  249.      ;; Turn the first integer we match into a wildcard.
  250.      ;; This is pretty dubious...
  251.      (setq font
  252.            (concat (substring font 0 (match-beginning 1)) "*"
  253.                (substring font (match-end 1) (match-end 0))))))
  254.   (sort
  255.    (delq nil
  256.      (mapcar (function
  257.           (lambda (name)
  258.             (and (string-match x-font-regexp name)
  259.              (list
  260.               (string-to-int (substring name (match-beginning 5)
  261.                             (match-end 5)))
  262.               (string-to-int (substring name (match-beginning 6)
  263.                             (match-end 6)))
  264.               name))))
  265.          (list-fonts font device)))
  266.    (function (lambda (x y) (if (= (nth 1 x) (nth 1 y))
  267.                    (< (nth 0 x) (nth 0 y))
  268.                    (< (nth 1 x) (nth 1 y)))))))
  269.  
  270. ;; Given a font name, this attempts to construct a valid font name for
  271. ;; DEVICE whose size is the next smaller (if UP-P is nil) or larger
  272. ;; (if UP-P is t) size and whose other characteristics are the same
  273. ;; as the given font.
  274.  
  275. (defun x-frob-font-size (font up-p device)
  276.   (if (stringp font) (setq font (make-font-instance font device)))
  277.   (if (font-instance-p font) (setq font (font-instance-truename font)))
  278.   (let ((available (and font
  279.             (x-available-font-sizes font device))))
  280.     (cond
  281.      ((null available) nil)
  282.      ((or (= 0 (nth 0 (car available)))
  283.       (= 0 (nth 1 (car available))))
  284.       ;; R5 scalable fonts: change size by 1 point.
  285.       ;; If they're scalable the first font will have pixel or point = 0.
  286.       ;; Sometimes one is 0 and the other isn't (if it's a bitmap font that
  287.       ;; can be scaled), sometimes both are (if it's a true outline font).
  288.       (let ((name (nth 2 (car available)))
  289.         old-size)
  290.     (or (string-match x-font-regexp font) (error "can't parse %S" font))
  291.     (setq old-size (string-to-int
  292.             (substring font (match-beginning 6) (match-end 6))))
  293.     (or (> old-size 0) (error "font truename has 0 pointsize?"))
  294.     (or (string-match x-font-regexp name) (error "can't parse %S" name))
  295.     ;; turn pixelsize into a wildcard, and make pointsize be +/- 10,
  296.     ;; which is +/- 1 point.  All other fields stay the same as they
  297.     ;; were in the "template" font returned by x-available-font-sizes.
  298.     ;;
  299.     ;; #### But this might return the same font: for example, if the
  300.     ;;      truename of "-*-courier-medium-r-normal--*-230-75-75-m-0-*"
  301.     ;;      is "...-240-..." (instead of 230) then this loses, because
  302.     ;;      the 230 that was passed in as an arg got turned into 240
  303.     ;;      by the call to font-instance-truename; then we decrement that
  304.     ;;    by 10 and return the result which is the same.  I think the
  305.     ;;    way to fix this is to make this be a loop that keeps trying
  306.     ;;      progressively larger pointsize deltas until it finds one
  307.     ;;      whose truename differs.  Have to be careful to avoid infinite
  308.     ;;      loops at the upper end...
  309.     ;;
  310.     (concat (substring name 0 (match-beginning 5)) "*"
  311.         (substring name (match-end 5) (match-beginning 6))
  312.         (int-to-string (+ old-size (if up-p 10 -10)))
  313.         (substring name (match-end 6) (match-end 0)))))
  314.      (t
  315.       ;; non-scalable fonts: take the next available size.
  316.       (let ((rest available)
  317.         (last nil)
  318.         result)
  319.     (setq font (downcase font))
  320.     (while rest
  321.       (cond ((and (not up-p) (equal font (downcase (nth 2 (car rest)))))
  322.          (setq result last
  323.                rest nil))
  324.         ((and up-p (equal font (and last (downcase (nth 2 last)))))
  325.          (setq result (car rest)
  326.                rest nil)))
  327.       (setq last (car rest))
  328.       (setq rest (cdr rest)))
  329.     (nth 2 result))))))
  330.  
  331. (defun x-find-smaller-font (font &optional device)
  332.   "Loads a new, slightly smaller version of the given font (or font name).
  333. Returns the font if it succeeds, nil otherwise.
  334. If scalable fonts are available, this returns a font which is 1 point smaller.
  335. Otherwise, it returns the next smaller version of this font that is defined."
  336.   (x-frob-font-size font nil device))
  337.  
  338. (defun x-find-larger-font (font &optional device)
  339.   "Loads a new, slightly larger version of the given font (or font name).
  340. Returns the font if it succeeds, nil otherwise.
  341. If scalable fonts are available, this returns a font which is 1 point larger.
  342. Otherwise, it returns the next larger version of this font that is defined."
  343.   (x-frob-font-size font t device))
  344.  
  345. (defalias 'x-make-face-bold 'make-face-bold)
  346. (defalias 'x-make-face-italic 'make-face-italic)
  347. (defalias 'x-make-face-bold-italic 'make-face-bold-italic)
  348. (defalias 'x-make-face-unbold 'make-face-unbold)
  349. (defalias 'x-make-face-unitalic 'make-face-unitalic)
  350.  
  351. (make-obsolete 'x-make-face-bold 'make-face-bold)
  352. (make-obsolete 'x-make-face-italic 'make-face-italic)
  353. (make-obsolete 'x-make-face-bold-italic 'make-face-bold-italic)
  354. (make-obsolete 'x-make-face-unbold 'make-face-unbold)
  355. (make-obsolete 'x-make-face-unitalic 'make-face-unitalic)
  356.  
  357.  
  358. ;; Define some logical color names to be used when reading the pixmap files.
  359. (if (featurep 'xpm)
  360.     (setq xpm-color-symbols
  361.       (list
  362.        (purecopy '("foreground" (face-foreground 'default)))
  363.        (purecopy '("background" (face-background 'default)))
  364.        (purecopy '("backgroundToolBarColor"
  365.                (x-get-resource "backgroundToolBarColor"
  366.                        "BackgroundToolBarColor" 'string)))
  367.        )))
  368.  
  369. ;;; internal routines
  370.  
  371. ;;; x-init-face-from-resources is responsible for initializing a
  372. ;;; newly-created face from the resource database.
  373. ;;;
  374. ;;; When a new frame is created, it is called from `x-init-frame-faces'
  375. ;;; called from `init-frame-faces' called from init_frame_faces()
  376. ;;; from Fmake_frame().  In this case it is called once for each existing
  377. ;;; face, with the newly-created frame as the argument.  It then initializes
  378. ;;; the newly-created faces on that frame.
  379. ;;;
  380. ;;; It's also called from `init-device-faces' and
  381. ;;; `init-global-faces'.
  382. ;;;
  383. ;;; This had better not signal an error.  The frame is in an intermediate
  384. ;;; state where signalling an error or entering the debugger would likely
  385. ;;; result in a crash.
  386.  
  387. (defun x-init-face-from-resources (face &optional locale set-anyway)
  388.  
  389.   ;;
  390.   ;; These are things like "attributeForeground" instead of simply
  391.   ;; "foreground" because people tend to do things like "*foreground",
  392.   ;; which would cause all faces to be fully qualified, making faces
  393.   ;; inherit attributes in a non-useful way.  So we've made them slightly
  394.   ;; less obvious to specify in order to make them work correctly in
  395.   ;; more random environments.
  396.   ;;
  397.   ;; I think these should be called "face.faceForeground" instead of
  398.   ;; "face.attributeForeground", but they're the way they are for
  399.   ;; hysterical reasons. (jwz)
  400.  
  401.   (let* ((append (if set-anyway nil 'append))
  402.      (face-sym (face-name face))
  403.      (name (symbol-name face-sym))
  404.      (fn (x-get-resource-and-maybe-bogosity-check
  405.           (concat name ".attributeFont")
  406.           "Face.AttributeFont"
  407.           'string locale))
  408.      (fg (x-get-resource-and-maybe-bogosity-check
  409.           (concat name ".attributeForeground")
  410.           "Face.AttributeForeground"
  411.           'string locale))
  412.      (bg (x-get-resource-and-maybe-bogosity-check
  413.           (concat name ".attributeBackground")
  414.           "Face.AttributeBackground"
  415.           'string locale))
  416.      (bgp (x-get-resource-and-maybe-bogosity-check
  417.            (concat name ".attributeBackgroundPixmap")
  418.            "Face.AttributeBackgroundPixmap"
  419.            'string locale))
  420.      (ulp (x-get-resource-and-maybe-bogosity-check
  421.            (concat name ".attributeUnderline")
  422.            "Face.AttributeUnderline"
  423.            'boolean locale))
  424.      (stp (x-get-resource-and-maybe-bogosity-check
  425.            (concat name ".attributeStrikethru")
  426.            "Face.AttributeStrikethru"
  427.            'boolean locale))
  428.      ;; we still resource for these TTY-only resources so that
  429.      ;; you can specify resources for TTY frames/devices.  This is
  430.      ;; useful when you start up your XEmacs on an X display and later
  431.      ;; open some TTY frames.
  432.      (hp (x-get-resource-and-maybe-bogosity-check
  433.           (concat name ".attributeHighlight")
  434.           "Face.AttributeHighlight"
  435.           'boolean locale))
  436.      (dp (x-get-resource-and-maybe-bogosity-check
  437.           (concat name ".attributeDim")
  438.           "Face.AttributeDim"
  439.           'boolean locale))
  440.      (bp (x-get-resource-and-maybe-bogosity-check
  441.           (concat name ".attributeBlinking")
  442.           "Face.AttributeBlinking"
  443.           'boolean locale))
  444.      (rp (x-get-resource-and-maybe-bogosity-check
  445.           (concat name ".attributeReverse")
  446.           "Face.AttributeReverse"
  447.           'boolean locale))
  448.      )
  449.  
  450.     ;;
  451.     ;; If this is the default face, then any unspecified properties should
  452.     ;; be defaulted from the global properties.  Can't do this for
  453.     ;; frames or devices because then, common resource specs like
  454.     ;; "*Foreground: black" will have unwanted effects.
  455.     ;;
  456.     (if (and (eq (face-name face) 'default)
  457.          (or (null locale) (eq locale 'global)))
  458.     (progn
  459.       (or fn (setq fn (x-get-resource
  460.                "font" "Font" 'string locale)))
  461.       (or fg (setq fg (x-get-resource
  462.                "foreground" "Foreground" 'string locale)))
  463.       (or bg (setq bg (x-get-resource
  464.                "background" "Background" 'string locale)))))
  465.     ;;
  466.     ;; "*cursorColor: foo" is equivalent to setting the background of the
  467.     ;; text-cursor face.
  468.     ;;
  469.     (if (and (eq (face-name face) 'text-cursor)
  470.          (or (null locale) (eq locale 'global)))
  471.     (setq bg (or (x-get-resource
  472.               "cursorColor" "CursorColor" 'string locale) bg)))
  473.     ;; #### should issue warnings?  I think this should be
  474.     ;; done when the instancing actually happens, but I'm not
  475.     ;; sure how it should actually be dealt with.
  476.     (if fn
  477.     (set-face-font face fn locale nil append))
  478.     ;; Kludge-o-rooni.  Set the foreground and background resources for
  479.     ;; X devices only -- otherwise things tend to get all messed up
  480.     ;; if you start up an X frame and then later create a TTY frame.
  481.     (if fg
  482.     (set-face-foreground face fg locale 'x append))
  483.     (if bg
  484.     (set-face-background face bg locale 'x append))
  485.     (if bgp
  486.     (set-face-background-pixmap face bgp locale nil append))
  487.     (if ulp
  488.     (set-face-underline-p face ulp locale nil append))
  489.     (if stp
  490.     (set-face-strikethru-p face stp locale nil append))
  491.     (if hp
  492.     (set-face-highlight-p face hp locale nil append))
  493.     (if dp
  494.     (set-face-dim-p face dp locale nil append))
  495.     (if bp
  496.     (set-face-blinking-p face bp locale nil append))
  497.     (if rp
  498.     (set-face-reverse-p face rp locale nil append))
  499.     ))
  500.  
  501. ;; GNU Emacs compatibility. (move to obsolete.el?)
  502. (defalias 'make-face-x-resource-internal 'x-init-face-from-resources)
  503.  
  504. ;;; x-init-global-faces is responsible for ensuring that the
  505. ;;; default face has some reasonable fallbacks if nothing else is
  506. ;;; specified.
  507. ;;;
  508. (defun x-init-global-faces ()
  509.   (or (face-font 'default 'global)
  510.       (set-face-font 'default
  511.              "-*-courier-medium-r-*-*-*-120-*-*-*-*-iso8859-*")
  512.       'global)
  513.   (or (face-foreground 'default 'global)
  514.       (set-face-foreground 'default "black" 'global 'x))
  515.   (or (face-background 'default 'global)
  516.       (set-face-background 'default "gray80" 'global 'x)))
  517.  
  518. ;;; x-init-device-faces is responsible for initializing default
  519. ;;; values for faces on a newly created device.
  520. ;;;
  521. (defun x-init-device-faces (device)
  522.   ;;
  523.   ;; If the "default" face didn't have a font specified, try to pick one.
  524.   ;;
  525.   (or
  526.    (face-font-instance 'default device)
  527.    ;;
  528.    ;; No font specified in the resource database; try to cope.
  529.    ;;
  530.    ;; At first I wanted to do this by just putting a font-spec in the
  531.    ;; fallback resources passed to XtAppInitialize(), but that fails
  532.    ;; if there is an Emacs app-defaults file which doesn't specify a
  533.    ;; font: apparently the fallback resources are not consulted when
  534.    ;; there is an app-defaults file, which seems pretty bogus to me.
  535.    ;;
  536.    ;; We should also probably try "*xtDefaultFont", but I think that it
  537.    ;; might be legal to specify that as "xtDefaultFont:", that is, at
  538.    ;; top level, instead of "*xtDefaultFont:", that is, applicable to
  539.    ;; every application.  `x-get-resource' can't handle that right now.
  540.    ;; Anyway, xtDefaultFont is probably variable-width.
  541.    ;;
  542.    ;; Some who have LucidaTypewriter think it's a better font than Courier,
  543.    ;; but it has the bug that there are no italic and bold italic versions.
  544.    ;; We could hair this code up to try and mix-and-match fonts to get a
  545.    ;; full complement, but really, why bother.  It's just a default.
  546.    ;;
  547.    (let (new-x-font)
  548.      (setq new-x-font (or
  549.       ;;
  550.       ;; We default to looking for iso8859 fonts.  Using a wildcard for the
  551.       ;; encoding would be bad, because that can cause English speakers to get
  552.       ;; Kanji fonts by default.  It is safe to assume that people using a
  553.       ;; language other than English have both set $LANG, and have specified
  554.       ;; their `font' and `fontList' resources.  In any event, it's better to
  555.       ;; err on the side of the English speaker in this case because they are
  556.       ;; much less likely to have encountered this problem, and are thus less
  557.       ;; likely to know what to do about it.
  558.  
  559.       ;; Try for Courier.  Almost everyone has that.  (Does anyone not?)
  560.       (make-font-instance
  561.        "-*-courier-medium-r-*-*-*-120-*-*-*-*-iso8859-*" device t)
  562.       (make-font-instance
  563.        "-*-courier-*-r-*-*-*-120-*-*-*-*-iso8859-*" device t)
  564.       ;; Next try for any "medium" charcell or monospaced iso8859 font.
  565.       (make-font-instance "-*-*-medium-r-*-*-*-120-*-*-m-*-iso8859-*" device t)
  566.       (make-font-instance "-*-*-medium-r-*-*-*-120-*-*-c-*-iso8859-*" device t)
  567.       ;; Next try for any charcell or monospaced iso8859 font.
  568.       (make-font-instance "-*-*-*-r-*-*-*-120-*-*-m-*-iso8859-*" device t)
  569.       (make-font-instance "-*-*-*-r-*-*-*-120-*-*-c-*-iso8859-*" device t)
  570.       ;; Ok, let's at least try to stay in 8859...
  571.       (make-font-instance "-*-*-*-r-*-*-*-120-*-*-*-*-iso8859-*" device t)
  572.       ;; Boy, we sure are losing now.  Try the above, but in any encoding.
  573.       (make-font-instance "-*-*-medium-r-*-*-*-120-*-*-m-*-*-*" device t)
  574.       (make-font-instance "-*-*-medium-r-*-*-*-120-*-*-c-*-*-*" device t)
  575.       (make-font-instance "-*-*-*-r-*-*-*-120-*-*-m-*-*-*" device t)
  576.       (make-font-instance "-*-*-*-r-*-*-*-120-*-*-c-*-*-*" device t)
  577.       (make-font-instance "-*-*-*-r-*-*-*-120-*-*-*-*-*-*" device t)
  578.       ;; Hello?  Please?
  579.       (make-font-instance "-*-*-*-*-*-*-*-120-*-*-*-*-*-*" device t)
  580.       (make-font-instance "*" device t)
  581.       ;; if we get to here we're screwed, and faces.c will fatal()...
  582.       ))
  583.      (if (not (face-font 'default 'global))
  584.      (set-face-font 'default new-x-font)
  585.        (set-face-font 'default new-x-font device))))
  586.   ;;
  587.   ;; If the "default" face didn't have both colors specified, then pick
  588.   ;; some, taking into account whether one of the colors was specified.  
  589.   ;;
  590.   (let ((fg (face-foreground-instance 'default device))
  591.     (bg (face-background-instance 'default device)))
  592.     (if (not (and fg bg))
  593.     (if (or (and fg (equal (downcase (color-instance-name fg)) "white"))
  594.         (and bg (equal (downcase (color-instance-name bg)) "black")))
  595.         (progn
  596.           (or fg (set-face-foreground 'default "white" device))
  597.           (or bg (set-face-background 'default "black" device)))
  598.       (or fg (set-face-foreground 'default "white" device))
  599.       (or bg (set-face-background 'default "black" device)))))
  600.  
  601.   ;; Don't look at reverseVideo now or initialize the modeline.  This
  602.   ;; is done on a per-frame basis at the appropriate time.
  603.  
  604.   ;;
  605.   ;; Now let's try to pick some reasonable defaults for a few other faces.
  606.   ;; This kind of stuff should normally go on the create-frame-hook, but
  607.   ;; this way we won't be in danger of the user screwing things up by not
  608.   ;; adding hooks in a safe way.
  609.   ;;
  610.   (x-init-pointer-shape device)  ; from x-mouse.el
  611.     )
  612.  
  613. ;;; This is called from `init-frame-faces', which is called from 
  614. ;;; init_frame_faces() which is called from Fmake_frame(), to perform
  615. ;;; any device-specific initialization.
  616. ;;;
  617. (defun x-init-frame-faces (frame)
  618.   ;;
  619.   ;; The faces already got initialized (by init-frame-faces) from
  620.   ;; the resource database or global, non-frame faces.  The default,
  621.   ;; bold, bold-italic, and italic faces (plus various other random faces)
  622.   ;; got set up then.  But modeline didn't so that reverseVideo can be
  623.   ;; frame-specific.
  624.   ;;
  625.  
  626.   ;;
  627.   ;; If reverseVideo was specified, swap the foreground and background
  628.   ;; of the default and modeline faces.
  629.   ;;
  630.   (cond ((car (x-get-resource "reverseVideo" "ReverseVideo" 'boolean frame))
  631.      ;; First make sure the modeline has fg and bg, inherited from the
  632.      ;; current default face - for the case where only one is specified,
  633.      ;; so that invert-face doesn't do something weird.
  634.      (or (face-foreground 'modeline frame)
  635.          (set-face-foreground 'modeline
  636.                   (face-foreground-instance 'default frame)
  637.                   frame))
  638.      (or (face-background 'modeline frame)
  639.          (set-face-background 'modeline
  640.                   (face-background-instance 'default frame)
  641.                   frame))
  642.      ;; Now invert both of them.  If they end up looking the same,
  643.      ;; make-frame-initial-faces will invert the modeline again later.
  644.      (invert-face 'default frame)
  645.      (invert-face 'modeline frame)
  646.      )))
  647.